package com.lgmshare.k3.ui.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.FontMetricsInt;
import android.graphics.Rect;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;

import androidx.appcompat.widget.AppCompatImageView;

import com.lgmshare.component.utils.UIUtils;
import com.lgmshare.k3.K3Config;
import com.lgmshare.k3.R;

import java.util.ArrayList;
import java.util.List;

public class ImageViewWithText extends AppCompatImageView {

    private DrawItem[] mDrawItems;
    private Item[] mItems;
    private Paint mTextPaint = new TextPaint();
    private int d;
    private int e;
    private int f;
    private int g;
    private int h;
    private int i;
    private int j;
    private List<String> mList;

    public ImageViewWithText(Context context) {
        super(context);
        init(context);
    }

    public ImageViewWithText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ImageViewWithText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextSize(24.0F);
        mTextPaint.setTextAlign(Align.LEFT);
        mTextPaint.setColor(getResources().getColor(R.color.red));
        FontMetricsInt fontMetricsInt = mTextPaint.getFontMetricsInt();
        log(fontMetricsInt.toString());
        g = (fontMetricsInt.descent - fontMetricsInt.ascent + fontMetricsInt.leading);
        h = fontMetricsInt.ascent;
        i = ((int) (mTextPaint.measureText(". ") + 0.5F));
        j = ((int) (mTextPaint.measureText("...") + 0.5F));
        StringBuilder builder = new StringBuilder("font height:");
        builder.append(g);
        log(builder.toString());
        e = UIUtils.dipToPx(12.0F);
        f = UIUtils.dipToPx(9.0F);
    }

    private Item a(String content, int paramInt1, int paramInt2, int paramInt3, int paramInt4, int paramInt5, int paramInt6) {
        Item item = new Item();
        item.a = content.substring(paramInt1, paramInt2 + paramInt1);
        item.b.left = paramInt3;
        item.b.top = paramInt4;
        item.b.right = (paramInt3 + paramInt5);
        item.b.bottom = (paramInt4 + paramInt6);
        return item;
    }

    private String a(String content) {
        int i1 = content.length();
        int n = mTextPaint.breakText(content, 0, i1, false, j, null);
        StringBuilder builder = new StringBuilder("replace text with dash, count:");
        builder.append(n);
        log(builder.toString());
        int m = n;
        if (n == 0) {
            m = 1;
        }
        if ((m > 0) && (m < i1)) {
            builder = new StringBuilder();
            builder.append(content.substring(0, i1 - m));
            builder.append("...");
            return builder.toString();
        }
        return content;
    }

    private void a(Item item, Canvas canvas) {
        String str = item.a;
        Rect rect = item.b;
        canvas.save();
        canvas.clipRect(rect);
        if (item.c) {
            canvas.drawText(str, rect.left, (rect.top + rect.bottom - g) / 2 - h, mTextPaint);
        } else {
            canvas.drawText(str, rect.left, rect.top - h, mTextPaint);
        }
        canvas.restore();
    }

    private void a(List<String> list, int paramInt1, int paramInt2) {
        int i4 = getPaddingLeft();
        int i3 = getPaddingRight();
        int m = getPaddingBottom();
        int i2 = getPaddingTop();
        int i7 = e;
        int i6 = i;
        int i5 = i4 + i7 + i6;
        int i1 = paramInt2 - i7 * 2 - i2 - m;
        StringBuilder builder = new StringBuilder("width:");
        builder.append(paramInt1);
        builder.append(", height:");
        builder.append(paramInt2);
        log(builder.toString());
        if ((paramInt1 > 0) && (paramInt2 > 0)) {
            Rect rect = new Rect();
            paramInt2 = i2 + i7;
            m = 0;
            int n;
            while (m < list.size()) {
                String str = list.get(m);
                n = paramInt2;
                if (!TextUtils.isEmpty(str)) {
                    DrawItem drawItem = new DrawItem();
                    rect.left = i5;
                    rect.top = paramInt2;
                    rect.right = (i5 + (paramInt1 - i7 * 2 - i4 - i3 - i6));
                    rect.bottom = (paramInt2 + i1);
                    if (!a(str, 0, str.length(), rect, drawItem)) {
                        d += 1;
                        mDrawItems[m] = drawItem;
                        break;
                    }
                    if (drawItem.b != null) {
                        paramInt2 = drawItem.b.b.bottom + f;
                    } else if (drawItem.a != null) {
                        paramInt2 = drawItem.a.b.bottom + f;
                    }
                    d += 1;
                    mDrawItems[m] = drawItem;
                    n = paramInt2;
                }
                m++;
                paramInt2 = n;
            }
            if (mDrawItems.length > 0) {
                paramInt1 = d;
                if (paramInt1 > 0) {
                    mItems = new Item[paramInt1];
                    n = i2 + e;
                    paramInt1 = n;
                    for (m = 0; m < d; m++) {
                        DrawItem drawItem = mDrawItems[m];
                        paramInt2 = paramInt1;
                        if (drawItem.a != null) {
                            paramInt2 = Math.max(paramInt1, drawItem.a.b.bottom);
                        }
                        paramInt1 = paramInt2;
                        if (drawItem.b != null) {
                            paramInt1 = Math.max(paramInt2, drawItem.b.b.bottom);
                        }
                    }
                    paramInt2 = (i1 - (paramInt1 - n)) / 2;
                    for (paramInt1 = 0; paramInt1 < d; paramInt1++) {
                        DrawItem drawItem = mDrawItems[paramInt1];
                        if (drawItem.a != null) {
                            drawItem.a.b.offset(0, paramInt2);
                            Rect rect1 = drawItem.a.b;
                            mItems[paramInt1] = a(". ", 0, 2, rect1.left - i, (rect1.top + rect1.bottom) / 2 + h, i, g);
                        }
                        if (drawItem.b != null) {
                            drawItem.b.b.offset(0, paramInt2);
                        }
                    }
                }
            }
        }
    }

    private boolean a(String content, int paramInt1, int paramInt2, Rect rect, DrawItem drawItem) {
        Rect localRect = new Rect();
        paramInt1 = rect.width();
        if (localRect.height() > rect.height()) {
            return false;
        }
        Object localObject = mTextPaint;
        float f1 = paramInt1;
        int i1 = ((Paint) localObject).breakText(content, 0, paramInt2, true, f1, null);
        if (i1 == 0) {
            return false;
        }
        localObject = new StringBuilder("first line count:");
        ((StringBuilder) localObject).append(i1);
        log(localObject.toString());
        int n = rect.top;
        int m = rect.left;
        drawItem.a = a(content, 0, i1, m, n, paramInt1, g);
        if (i1 == paramInt2 + 0) {
            Rect rect1 = drawItem.a.b;
            rect1.bottom += g;
            drawItem.a.c = true;
            return true;
        }
        n += drawItem.a.b.height();
        if (n + localRect.height() > rect.bottom) {
            drawItem.a.a = a(drawItem.a.a);
            Rect rect1 = drawItem.a.b;
            rect1.bottom += g;
            drawItem.a.c = true;
            return false;
        }
        i1 += 0;
        int i2 = mTextPaint.breakText(content, i1, paramInt2, true, f1, null);
        if (i2 == 0) {
            Rect rect1 = drawItem.a.b;
            rect1.bottom += g;
            drawItem.a.c = true;
            return false;
        }
        StringBuilder builder = new StringBuilder("second line count:");
        builder.append(i2);
        log(builder.toString());
        drawItem.b = a(content, i1, i2, m, n, paramInt1, g);
        if (i2 < paramInt2 - i1) {
            drawItem.b.a = a(drawItem.b.a);
        }
        return true;
    }

    private static void log(String s) {
        if (K3Config.DEBUG_MODE) {
            Log.d("ImageViewWithText", s);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int n = 0;
        int m;
        if ((mDrawItems != null) && (mDrawItems.length > 0) && (d > 0)) {
            for (m = 0; ; m++) {
                if ((m >= mDrawItems.length) || (m >= d)) {
                    break;
                }
                DrawItem drawItem = mDrawItems[m];
                if (drawItem.a != null) {
                    a(drawItem.a, canvas);
                }
                if (drawItem.b != null) {
                    a(drawItem.b, canvas);
                }
            }
        }
        if ((mItems != null) && (mItems.length > 0) && (d > 0)) {
            int i1 = mItems.length;
            for (m = n; m < i1; m++) {
                a(mItems[m], canvas);
            }
        }
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if ((mList != null) && (mList.size() > 0)) {
            a(mList, w, h);
        }
    }

    public void setContent(List<String> list) {
        if ((list != null) && (list.size() > 0)) {
            if (mList == null) {
                mList = new ArrayList();
            }
            mList.clear();
            mList.addAll(list);
            d = 0;
            mDrawItems = new DrawItem[list.size()];
            a(mList, getWidth(), getHeight());
            invalidate();
            return;
        }
        if (mList != null) {
            mList.clear();
        }
        mList = null;
        d = 0;
        mDrawItems = null;
        mItems = null;
        invalidate();
    }

    class DrawItem {
        Item a;
        Item b;

        private DrawItem() {
        }
    }

    class Item {
        String a;
        Rect b = new Rect();
        boolean c = false;

        public Item() {
        }
    }
}